package com.liyong.bos.controller;

import com.liyong.bos.domain.BcStaff;
import com.liyong.bos.service.GetStaffService;
import com.liyong.bos.utils.JsonResult;
import com.liyong.bos.utils.TableJsonData;
import com.liyong.bos.vo.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by ApacheMoy on 2017/4/8.
 */
@Controller
@RequestMapping(value = "/bcStaff")
public class GetStaffController {
    @Autowired
    private GetStaffService getStaffService;
    @RequestMapping(value = "/getAllBcStaff")
    public @ResponseBody
    TableJsonData getAllBcStaff(PageInfo pageInfo, HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Pragma", "no-cache");
        List<BcStaff> bcStaffs=getStaffService.findAllBcStaff("",pageInfo.getPage(),pageInfo.getRows());
        int total=getStaffService.countByPage(null,null);
        TableJsonData tableJsonData=new TableJsonData();
        tableJsonData.setTotal(total);
        tableJsonData.setRows(bcStaffs);
        return tableJsonData;
    }

    /**
     * 增加取派员
     * */
    @RequestMapping(value = "/addBcStaff")
    public @ResponseBody JsonResult addBcStaff(BcStaff bcStaff){
        JsonResult jsonResult=new JsonResult();
        if(StringUtils.isNotBlank(bcStaff.getName())){
            getStaffService.insertBcStaff(bcStaff);
            jsonResult.setMsg("success");
            return jsonResult;
        }
        jsonResult.setMsg("error");
        return jsonResult;
    }

    /**
     *修改取派员信息
     **/
    @RequestMapping(value = "/updateBcStaff")
    public @ResponseBody JsonResult updateBcStaff(BcStaff bcStaff){
        System.out.println("------");
        JsonResult jsonResult=new JsonResult();
        if(StringUtils.isNotBlank(bcStaff.getId())){
            getStaffService.updateBcStaff(bcStaff);
            System.out.println("---+---");
            jsonResult.setMsg("success");
            return jsonResult;
        }
        jsonResult.setMsg("error");
        return jsonResult;
    }

    /**
     *删除取派员
     **/
    @RequestMapping(value = "/deleteBcStaffs")
    public @ResponseBody JsonResult deleteBcStaffs(@RequestBody List<String> id){
        JsonResult jsonResult=new JsonResult();
        try {
            for(String ids:id){
                getStaffService.deleteBcStaffs(ids);
            }
            jsonResult.setMsg("success");
        }catch (Exception e){
            jsonResult.setMsg("error");
        }
        return jsonResult;
    }

}
